<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车次id" prop="trainId">
      <el-input v-model="dataForm.trainId" placeholder="车次id"></el-input>
    </el-form-item>
    <el-form-item label="车次编码，比如T31" prop="trainCode">
      <el-input v-model="dataForm.trainCode" placeholder="车次编码，比如T31"></el-input>
    </el-form-item>
    <el-form-item label="" prop="carriageId">
      <el-input v-model="dataForm.carriageId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="carriageNumber">
      <el-input v-model="dataForm.carriageNumber" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="seatId">
      <el-input v-model="dataForm.seatId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="座位编码" prop="seatCode">
      <el-input v-model="dataForm.seatCode" placeholder="座位编码"></el-input>
    </el-form-item>
    <el-form-item label="价格系数" prop="priceFactor">
      <el-input v-model="dataForm.priceFactor" placeholder="价格系数"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          trainId: '',
          trainCode: '',
          carriageId: '',
          carriageNumber: '',
          seatId: '',
          seatCode: '',
          priceFactor: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          trainId: [
            { required: true, message: '车次id不能为空', trigger: 'blur' }
          ],
          trainCode: [
            { required: true, message: '车次编码，比如T31不能为空', trigger: 'blur' }
          ],
          carriageId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          carriageNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          seatId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          seatCode: [
            { required: true, message: '座位编码不能为空', trigger: 'blur' }
          ],
          priceFactor: [
            { required: true, message: '价格系数不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ticket/tickettemplate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.trainId = data.ticketTemplate.trainId
                this.dataForm.trainCode = data.ticketTemplate.trainCode
                this.dataForm.carriageId = data.ticketTemplate.carriageId
                this.dataForm.carriageNumber = data.ticketTemplate.carriageNumber
                this.dataForm.seatId = data.ticketTemplate.seatId
                this.dataForm.seatCode = data.ticketTemplate.seatCode
                this.dataForm.priceFactor = data.ticketTemplate.priceFactor
                this.dataForm.createTime = data.ticketTemplate.createTime
                this.dataForm.updateTime = data.ticketTemplate.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ticket/tickettemplate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'trainId': this.dataForm.trainId,
                'trainCode': this.dataForm.trainCode,
                'carriageId': this.dataForm.carriageId,
                'carriageNumber': this.dataForm.carriageNumber,
                'seatId': this.dataForm.seatId,
                'seatCode': this.dataForm.seatCode,
                'priceFactor': this.dataForm.priceFactor,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
